/**
* @ProjectName: ShopSytem
* @Title: AddOrder.java 
* @Package com.element5th.shop.servlet.good 
* @Description: 提交订单
* @author 第五元素小组   
* @date 2012-10-26 16:21:38 
* @version V1.0   
*/
package com.element5th.shop.servlet.order;

import java.io.IOException;

import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.element5th.shop.comm.DaoFactory;
import com.element5th.shop.dto.BaseDto;
import com.element5th.shop.entity.ShopOrder;


/**
 * Servlet implementation class AddOrder
 */
@WebServlet("/page/user/add_order")
public class AddOrder extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * 将session中购物车列表提出，提交进订单，提交成功后清空购物车列表
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
	
		//---------------------------------------------------
		//============先获得订单页面传过来的数据信息，封装成DTO之后再传到数据库
		String RecieverName =request.getParameter("Order_RecieverName");

		String Order_Address =request.getParameter("Order_Address");

		String Order_Phone1=request.getParameter("Order_Phone1");

		String Order_Phone2=request.getParameter("Order_Phone2");
	
		String PayType_Id=request.getParameter("PayType_Id");
		String PayType =null;
		if(PayType_Id.equals(1)){
			PayType="货到付款";
		}else if(PayType_Id.equals(2)){
			PayType="在线支付";
		}else{
			PayType="邮局付款";
		}
		System.out.println(PayType);
		String Order_Remark_id=request.getParameter("Order_Remark");
		String Order_Remark =null;
		if(Order_Remark_id.equals(1)){
			Order_Remark="只工作日送货(双休日、假日不用送)";
		}else if(Order_Remark_id.equals(2)){
			Order_Remark="工作日、双休日与假日均可送货";
		}else{
			Order_Remark="只双休日、假日送货(工作日不用送)";
		}
		
		/*==============查找下一个序列值
		 String sql="select SEQ_ORDER_LIST.nextval from dual";
		Connection conn = DBHelper.getConnection();
		PreparedStatement ps = null;
		ResultSet rs=null;
		int Order_Id;
		
		try {
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			Order_Id=rs.getInt(0);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		 */
		
		//=============从session中取出dto，再取出用户id
		int User_Id;
		/*BaseDto basedto=new BaseDto();
		basedto=(BaseDto) request.getSession().getAttribute("basedto");
		User_Id=basedto.getUser().getUserId();*/
		User_Id=3;
		
		//===============提交时间4
		Date Order_SubTime=new Date();
		
		//==============选择运输方式
		String orderShipno =request.getParameter("orderShipno");
		String orderShiptype=null;
		double orderShipfee;
		if(orderShipno.equals(1)){
			orderShiptype="申通";
			orderShipfee=10.0;
		}else if(orderShipno.equals(2)){
			orderShiptype="圆通";
			orderShipfee=10.0;
		}else if(orderShipno.equals(3)){
			orderShiptype="EMS";
			orderShipfee=15.0;
		}else{
			orderShiptype="邮政";
			orderShipfee=8.0;
		}
		
		
		//================封装订单dto
		ShopOrder shoporder=new BaseDto().getShopOrder();
		shoporder.setOrderAddress(Order_Address);
		
		shoporder.setOrderSubtime(Order_SubTime);
		if(Order_Phone1==null){
			shoporder.setOrderPhone(Order_Phone2);
		}else{
			shoporder.setOrderPhone(Order_Phone1);			
		}
		
		shoporder.setOrderRecievername(RecieverName);
		shoporder.setOrderRemark(Order_Remark);
		shoporder.setOrderShipno(orderShipno);
		shoporder.setOrderShiptype(orderShiptype);
		
		shoporder.setOrderShipfee(orderShipfee);//每种运输方式固定的分配运输费用
		String orderDiscount=request.getParameter("orderDiscount");
		if(orderDiscount!=null){
			shoporder.setOrderDiscount(Double.valueOf(orderDiscount));//取得优惠值
		}
		
		String OrderSumfee=request.getParameter("OrderSumfee");
		if(OrderSumfee!=null){
			shoporder.setOrderSumfee(Double.valueOf(OrderSumfee));//取得最终费用
		}
		
		shoporder.setPaytypeId(Integer.valueOf(PayType_Id));
		shoporder.setUserId(User_Id);
		
		System.out.println(shoporder.getOrderAddress());
		System.out.println(Order_SubTime);
		System.out.println(Order_Phone1);
		System.out.println(RecieverName);
		System.out.println(Order_Remark);
		System.out.println(orderShipno);
		System.out.println(orderShiptype);
		System.out.println(orderShipfee);
		System.out.println(PayType_Id);
		
		BaseDto  basedto1=new BaseDto();
		basedto1.setShopOrder(shoporder);
        DaoFactory.getOrderDao().addOrder(basedto1);
		
		//---------------------------------------------------
		//===========提交成功后清空购物车列表
	}

}
